package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.controller.request.BookPageRequest;
import com.example.springboot.entity.Book;
import com.example.springboot.service.impl.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {


    @Autowired
    BookService bookService;


    private final String BASE_FILE_PATH = System.getProperty("user.dir") + "/files/";



    //保存
    @PostMapping("/save")
    public Result login(@RequestBody Book book) {
        bookService.save(book);
        return Result.success();
    }

    //更新
    @PutMapping("/update")
    public Result update(@RequestBody Book book) {
        bookService.update(book);
        return Result.success();
    }


    //获取
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Book admins = bookService.getById(id);
        return Result.success(admins);
    }

    //单个删除
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        bookService.deleteById(id);
        return Result.success();
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        bookService.deleteByIds(ids);
        return Result.success();
    }

    //   查询全部
    @GetMapping("/list")
    public Result list() {
        List<Book> users = bookService.list();
        return Result.success(users);
    }


    //    分页查询
    @GetMapping("/page")
    public Result page(BookPageRequest bookPageRequest) {
        return Result.success(bookService.page(bookPageRequest));
    }


}
